﻿@namespace AntBlazor.Internal
@inherits DatePickerPanelBase
@using System.Globalization;

@{
    var calendar = CultureInfo.InvariantCulture.Calendar;

    DateTime monthFirstDayDate = new DateTime(PickerValue.Year, PickerValue.Month, 1, 0, 0, 0);
    DayOfWeek monthFirstDayOfWeek = calendar.GetDayOfWeek(monthFirstDayDate);

    int diffDay = 1 - (int)monthFirstDayOfWeek;
    DateTime startDate = monthFirstDayDate.AddDays(diffDay);
}

<div class='@(DatePicker.IsShowTime || Picker == DatePickerType.Time ? $"{DatePicker.PrefixCls}-datetime-panel" : "")'>
    @if (DatePicker.IsShowTime)
    {
        <DatePickerTemplate PickerIndex="@PickerIndex"
                               MaxRow="6"
                               MaxCol="7"
                               ViewStartDate="startDate"
                               ShowFooter="@DatePicker.ShowToday"
                               IsInView="date => DateHelper.IsSameMonth(date, PickerValue)"
                               IsToday="date => DateHelper.IsSameDay(date, DatePicker.CurrentDate)"
                               IsSelected="date => DateHelper.IsSameDay(date, Value)"
                               GetColTitle='date => date.ToString("yyyy-MM-dd")'
                               OnValueSelect="OnSelectDate"
                               GetNextColValue="date => date.AddDays(1)">
            <RenderPickerHeader>
                <DatePickerHeader PickerIndex="@PickerIndex" />
            </RenderPickerHeader>
            <RenderTableHeader>
                <tr>
                    <th>一</th>
                    <th>二</th>
                    <th>三</th>
                    <th>四</th>
                    <th>五</th>
                    <th>六</th>
                    <th>日</th>
                </tr>
            </RenderTableHeader>
            <RenderColValue Context="currentColDate">
                @currentColDate.Day
            </RenderColValue>
        </DatePickerTemplate>
    }

    @{
        var startTime = new DateTime(2020, 1, 1, 0, 0, 0);
        var timeFormat = DatePicker.ShowTimeFormat;

        if (Picker == DatePickerType.Time && !string.IsNullOrEmpty(DatePicker.Format))
        {
            timeFormat = DatePicker.Format;
        }

        DatePickerDisabledTime disabledTime = GetDisabledTime();
    }
    <div class="@(DatePicker.PrefixCls)-time-panel">
        <div class="@(DatePicker.PrefixCls)-header">
            <div class="@(DatePicker.PrefixCls)-header-view">
                @Value.ToString(timeFormat) <br />
            </div>
        </div>
        <div class="@(DatePicker.PrefixCls)-content">
            @if (timeFormat.ToLower().Contains("hh"))
            {
                <ul class="@(DatePicker.PrefixCls)-time-panel-column" style="position: relative;">
                    @for (int hour = 0; hour < 24; hour++)
                    {
                        var viewTime = startTime;
                        bool disabled = disabledTime._disabledHours.Contains(hour);
                        string isSelectedCls = viewTime.Hour == Value.Hour ? $"{DatePicker.PrefixCls}-time-panel-cell-selected" : "";
                        string disabledCls = disabled ? $"{DatePicker.PrefixCls}-time-panel-cell-disabled" : "";

                        <li class="@(DatePicker.PrefixCls)-time-panel-cell @isSelectedCls @disabledCls">
                            <div class="@(DatePicker.PrefixCls)-time-panel-cell-inner"
                                 @onclick="e => { if (!disabled) OnSelectHour(viewTime); }">
                                @hour
                            </div>
                        </li>

                        startTime = startTime.AddHours(1);
                    }
                </ul>
            }
            @if (timeFormat.ToLower().Contains("mm"))
            {
                <ul class="@(DatePicker.PrefixCls)-time-panel-column" style="position: relative;">
                    @for (int minute = 0; minute < 60; minute++)
                    {
                        var viewTime = startTime;
                        bool disabled = disabledTime._disabledMinutes.Contains(minute);
                        string isSelectedCls = viewTime.Minute == Value.Minute ? $"{DatePicker.PrefixCls}-time-panel-cell-selected" : "";
                        string disabledCls = disabled ? $"{DatePicker.PrefixCls}-time-panel-cell-disabled" : "";

                        <li class="@(DatePicker.PrefixCls)-time-panel-cell @isSelectedCls @disabledCls">
                            <div class="@(DatePicker.PrefixCls)-time-panel-cell-inner"
                                 @onclick="e => { if (!disabled) OnSelectMinute(viewTime); }">
                                @minute
                            </div>
                        </li>

                        startTime = startTime.AddMinutes(1);
                    }
                </ul>
            }
            @if (timeFormat.ToLower().Contains("ss"))
            {
                <ul class="@(DatePicker.PrefixCls)-time-panel-column" style="position: relative;">
                    @for (int second = 0; second < 60; second++)
                    {
                        var viewTime = startTime;
                        bool disabled = disabledTime._disabledSeconds.Contains(second);
                        string isSelectedCls = viewTime.Second == Value.Second ? $"{DatePicker.PrefixCls}-time-panel-cell-selected" : "";
                        string disabledCls = disabled ? $"{DatePicker.PrefixCls}-time-panel-cell-disabled" : "";

                        <li class="@(DatePicker.PrefixCls)-time-panel-cell @isSelectedCls @disabledCls">
                            <div class="@(DatePicker.PrefixCls)-time-panel-cell-inner"
                                 @onclick="e => { if (!disabled) OnSelectSecond(viewTime); }">
                                @second
                            </div>
                        </li>

                        startTime = startTime.AddSeconds(1);
                    }
                </ul>
            }
        </div>
    </div>
</div>

@if (DatePicker.RenderExtraFooter != null)
{
    <div class="@(DatePicker.PrefixCls)-footer">
        <div class="@(DatePicker.PrefixCls)-footer-extra">
            @DatePicker.RenderExtraFooter
        </div>
    </div>
}

<div class="@(DatePicker.PrefixCls)-footer">
    <ul class="@(DatePicker.PrefixCls)-ranges">
        <li class="@(DatePicker.PrefixCls)-now">
            <a class="@(DatePicker.PrefixCls)-now-btn"
               @onclick="e => { OnSelectTime(DateTime.Now); DatePicker.Close(); }">
                此刻
            </a>
        </li>
        <li class="@(DatePicker.PrefixCls)-ok">
            <AntButton Type="@AntButtonType.Primary"
                       OnClick="e => DatePicker.Close()">
                确 定
            </AntButton>
        </li>
    </ul>
</div>